package models.accounts.vo;

import java.util.ArrayList;
import java.util.List;

import framework.base.BaseModel;
import framework.base.BaseQuery;
import framework.utils.StringUtil;
import framework.utils.UrlUtil;

/**
 * 账户查询对象
 * @author jinchaoyang
 */
public class AccountQuery extends BaseQuery {

    public String name;
    
    public String aliasName;
    
    public String state;
    
    
    @Override
    public String toQuery() {
        List<Object> result = toCountQuery();
        String sql = (String) result.get(0);
        sql = sql + " order by a.createdAt desc";
        return sql;
    }

    @Override
    public List<Object> toCountQuery() {
        StringBuffer sql = new StringBuffer("from Account a  where a.status=? ");
        List<Object> queryParams = new ArrayList<Object>();
        List<Object> args = new ArrayList<Object>();
        args.add(BaseModel.USE_STATUS);
        if(!StringUtil.isBlank(this.name)){
            sql.append(" and a.appName like ? ");
            args.add("%"+this.name+"%");
        }
        
        if(!StringUtil.isBlank(this.aliasName)){
            sql.append(" and a.aliasName like ? ");
            args.add("%"+this.aliasName+"%");
        }
        
        if(!StringUtil.isBlank(this.state)){
            sql.append(" and a.state = ? ");
            args.add(this.state);
        }
        
        queryParams.add(sql.toString());
        queryParams.add(args);
        return queryParams;
    }

    @Override
    public String toString() {
        return UrlUtil.toUrl(this, this.getClass()); 
    }

}
